package com.rg.survey.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.rg.survey.entity.ItemOptions;
import com.rg.survey.entity.SurveyItem;
import com.rg.survey.entity.Teacher;
import com.rg.survey.service.SurveyItemService;
import com.rg.survey.mapper.SurveyItemMapper;
import com.rg.survey.vo.P;
import com.rg.survey.vo.SurveyVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class SurveyItemServiceImpl extends ServiceImpl<SurveyItemMapper, SurveyItem>
    implements SurveyItemService{
    @Autowired
    ItemOptionsServiceImpl itemOptionsService;

    @Override
    public P selectPage(Map<String, Object> map) {
        LambdaQueryWrapper<SurveyItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(map.containsKey("surveyText")&& !StringUtils.isEmpty(map.get("surveyText").toString()),SurveyItem::getSurveyText,map.get("surveyText"));

        wrapper.orderByDesc(SurveyItem::getChecked,SurveyItem::getCreateTime);

        //判断分页
        if(map.containsKey("page")) {
            long current = Long.parseLong(map.get("page").toString());
//            System.out.println(current);
            long size = Long.parseLong(map.get("size").toString());
            Page<SurveyItem> page = new Page<>(current, size);//创建分页对象
            page = this.page(page, wrapper);
            List<SurveyItem> surveyItemList = page.getRecords();
            return new P(page.getTotal(), getSurveyVo(surveyItemList));
        }else{
            List<SurveyItem> surveyItemList = this.list(wrapper);
            
            return new P(0L,getSurveyVo(surveyItemList));
        }

    }

    @Override
    @Transactional
    public boolean insertSurvey(SurveyVo surveyVo) {
        //调查表中新增记录
        SurveyItem surveyItem = new SurveyItem();
        BeanUtils.copyProperties(surveyVo,surveyItem);
        this.save(surveyItem);
        //调查选项中新增多条记录
        List<ItemOptions> itemOptionsList = surveyVo.getItemOptionsList();
        itemOptionsList.forEach(itemOptions -> itemOptions.setItemId(surveyItem.getId()));
        itemOptionsService.saveBatch(itemOptionsList);
        return true;
    }

    @Override
    public boolean updateSurvey(SurveyVo surveyVo) {
        SurveyItem surveyItem = new SurveyItem();
        BeanUtils.copyProperties(surveyVo,surveyItem);
        this.updateById(surveyItem);//对调查表修改

        //对调查项表先移除，在新增
        List<ItemOptions> itemOptionsList = surveyVo.getItemOptionsList();
        LambdaQueryWrapper<ItemOptions> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ItemOptions::getItemId,surveyItem.getId());
        itemOptionsService.remove(wrapper);

        //新增记录
        itemOptionsList.forEach(itemOptions -> itemOptions.setItemId(surveyItem.getId()));
        itemOptionsService.saveBatch(itemOptionsList);

        return true;
    }

    private List<SurveyVo> getSurveyVo(List<SurveyItem> surveyItemList) {

        return surveyItemList.stream().map(surveyItem -> {
            SurveyVo vo = new SurveyVo();
            BeanUtils.copyProperties(surveyItem,vo);
            LambdaQueryWrapper<ItemOptions> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ItemOptions::getItemId,surveyItem.getId());
            List<ItemOptions> itemOptionsList = itemOptionsService.list(wrapper);
            vo.setItemOptionsList(itemOptionsList);
            return vo;
        }).collect(Collectors.toList());
    }
}




